package com.aishua.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.entity.emergencyPre.EmergencyEvacuationReqInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyPreDetailsRespInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyPreReqInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyPreRespInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyWarnReqInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyWarnStartUpReqInfo;
import com.aishua.fire.entity.emergencyPre.PositionReqInfo;
import com.aishua.fire.entity.emergencyPre.TroubleReqInfo;
import com.aishua.fire.entity.emergencyPre.TroubleRespInfo;
import com.aishua.fire.service.EmergencyPreManagerService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "应急预案管理", tags = { "应急预案管理相关接口" })
@RestController
@Slf4j
public class EmergencyPreManagerController {

	
	
	@Autowired
	private EmergencyPreManagerService emergencyPreService;

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加应急预案接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PostMapping(value = { "/pre" })
	public ResponseEntity<JsonObjectResult> addEmergencyInf(@RequestBody @Valid EmergencyPreReqInfo preInfo) {
		preInfo.setState("1");//初始状态为完成
		emergencyPreService.addEmergencyInf(preInfo);
		log.info("任务id:{}",preInfo.getId());
//		Map<String,Object> variables = new HashMap<String,Object>();
//		variables.put("pre_name", preInfo.getPre_name());
//		variables.put("iss_time",preInfo.getIss_time());
//		variables.put("state",preInfo.getState());
//		variables.put("department_id", preInfo.getDepartment_id());
//		ProcessInstance pi = runtimeService.startProcessInstanceByKey("plan",preInfo.getUser_id(),variables);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		
		return ResponseEntity.ok(r);
	}

	
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改应急预案接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PutMapping(value = { "/pre" })
	public ResponseEntity<JsonObjectResult> updateEmergencyInf(@RequestBody @Valid EmergencyPreReqInfo preInfo) {
		emergencyPreService.updateEmergencyInf(preInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除应急预案接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@DeleteMapping(value = { "/pre" })
	public ResponseEntity<JsonObjectResult> delEmergencyInf(@RequestBody @Valid EmergencyPreReqInfo preInfo) {
		log.info("删除应急预案请求参数:{}", preInfo.getId());
		emergencyPreService.delEmergencyInf(preInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}

	@ApiOperation(value = "分页查询应急预案接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@GetMapping(value = { "/pre" })
	public ResponseEntity<JsonArrayResult<EmergencyPreRespInfo>> emergencyPreList(
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "pre_name") String pre_name, @RequestParam(name = "pre_type") String pre_type) {
		JsonArrayResult<EmergencyPreRespInfo> r = null;
		List<EmergencyPreRespInfo> searchList = emergencyPreService.emergencyPreList(pageNumber, pageSize, pre_name,
				pre_type);
		if (searchList.size() > 0) {
			PageInfo<EmergencyPreRespInfo> pages = new PageInfo<EmergencyPreRespInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
			return ResponseEntity.ok(r);
		} else {
			r = JsonArrayResult.makeSuccessResult(searchList, 0);
			return ResponseEntity.ok(r);
		}
	}
	
	@PermMapping(noValidate = true)
	@ApiOperation(value="查询_预案详情接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@GetMapping(value = {"/pre/Details"} )
	public ResponseEntity<JsonArrayResult<EmergencyPreDetailsRespInfo>> preDetails(
			@RequestParam(name = "id") String id) {
		List<EmergencyPreDetailsRespInfo> searchList = emergencyPreService.preDetails(id);
		JsonArrayResult<EmergencyPreDetailsRespInfo> r = JsonArrayResult.makeSuccessResult(searchList, searchList.size());
			return ResponseEntity.ok(r);
		}

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加责任岗位信息接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PostMapping(value = { "/pre/position" })
	public ResponseEntity<JsonObjectResult> addPositionInf(@RequestBody @Valid PositionReqInfo preInfo) {

		emergencyPreService.addPositionInf(preInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "刪除责任岗位信息接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@DeleteMapping(value = { "/pre/position" })
	public ResponseEntity<JsonObjectResult> delPositionInf(@RequestBody @Valid PositionReqInfo preInfo) {

		emergencyPreService.delPositionInf(preInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改责任岗位信息接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PutMapping(value = { "/pre/position" })
	public ResponseEntity<JsonObjectResult> updatePositionInf(@RequestBody @Valid PositionReqInfo preInfo) {

		emergencyPreService.updatePositionInf(preInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加危险隐患接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PostMapping(value = { "/trouble" })
	public ResponseEntity<JsonObjectResult> addTroubleInf(@RequestBody @Valid TroubleReqInfo troubleInfo) {

		emergencyPreService.addTroubleInf(troubleInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改危险隐患接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@PutMapping(value = { "/trouble" })
	public ResponseEntity<JsonObjectResult> updateTroubleInf(@RequestBody @Valid TroubleReqInfo preInfo) {
		emergencyPreService.updateTroubleInf(preInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}

	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除危险隐患接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@DeleteMapping(value = { "/trouble" })
	public ResponseEntity<JsonObjectResult> delTroubleInf(@RequestBody @Valid TroubleReqInfo preInfo) {
		log.info("删除危险隐患接口请求参数:{}", preInfo.getId());
		emergencyPreService.delTroubleInf(preInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}

	@ApiOperation(value = "分页查询危险隐患接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@GetMapping(value = { "/trouble" })
	public ResponseEntity<JsonArrayResult<TroubleRespInfo>> troubleList(
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "department_id") String department_id,
			@RequestParam(name = "category") String category, @RequestParam(name = "start_time") String start_time,
			@RequestParam(name = "end_time") String end_time) {
		JsonArrayResult<TroubleRespInfo> r = null;
		List<TroubleRespInfo> searchList = emergencyPreService.troubleList(pageNumber, pageSize, department_id,
				start_time,end_time,category);
		if (searchList.size() > 0) {
			PageInfo<TroubleRespInfo> pages = new PageInfo<TroubleRespInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
			return ResponseEntity.ok(r);
		} else {
			r = JsonArrayResult.makeSuccessResult(searchList, 0);
			return ResponseEntity.ok(r);
		}
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "添加应急疏散场所接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
	@PostMapping(value = { "/evacuation" })
	public ResponseEntity<JsonObjectResult> addEvacuationInf(@RequestBody @Valid EmergencyEvacuationReqInfo evaInfo) {

		emergencyPreService.addEvacuationInf(evaInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "修改应急疏散场所接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
	@PutMapping(value = { "/evacuation" })
	public ResponseEntity<JsonObjectResult> updateEvacuationInf(@RequestBody @Valid EmergencyEvacuationReqInfo evaInfo) {

		emergencyPreService.updateEvacuationInf(evaInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "删除应急疏散场所接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
	@DeleteMapping(value = { "/evacuation" })
	public ResponseEntity<JsonObjectResult> delEvacuationInf(@RequestBody @Valid EmergencyEvacuationReqInfo evaInfo) {

		emergencyPreService.delEvacuationInf(evaInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	
	@ApiOperation(value = "分页查询应急疏散场所接口")
	@ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPRE)
	@GetMapping(value = { "/evacuation" })
	public ResponseEntity<JsonArrayResult<EmergencyEvacuationReqInfo>> evacuationList(
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "location") String location,@RequestParam(name = "object") String object) {
		JsonArrayResult<EmergencyEvacuationReqInfo> r = null;
		List<EmergencyEvacuationReqInfo> searchList = emergencyPreService.evacuationList(pageNumber, pageSize, location,object);
		if (searchList.size() > 0) {
			PageInfo<EmergencyEvacuationReqInfo> pages = new PageInfo<EmergencyEvacuationReqInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
			return ResponseEntity.ok(r);
		} else {
			r = JsonArrayResult.makeSuccessResult(searchList, 0);
			return ResponseEntity.ok(r);
		}
	}
	
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-添加预案标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @PostMapping(value = { "/warn" })
    public ResponseEntity<JsonObjectResult> addWarnInfo(@RequestBody @Valid EmergencyWarnReqInfo warnInfo) {
    
        emergencyPreService.addWarnInfo(warnInfo);
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-添加预案启动标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @PostMapping(value = { "/warn/startup" })
    public ResponseEntity<JsonObjectResult> addWarnStartUpInfo(@RequestBody @Valid EmergencyWarnStartUpReqInfo startInfo) {

        emergencyPreService.addWarnStartUpInfo(startInfo);
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
	
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-删除预案标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @DeleteMapping(value = { "/warn" })
    public ResponseEntity<JsonObjectResult> delwarnInf(@RequestBody @Valid EmergencyWarnReqInfo info) {

        emergencyPreService.delwarnInf(info.getId());
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-删除预案启动标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @DeleteMapping(value = { "/warn/startup" })
    public ResponseEntity<JsonObjectResult> delwarnstartupInf(@RequestBody @Valid EmergencyWarnStartUpReqInfo info) {

        emergencyPreService.delwarnstartupInf(info.getId());
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-修改预案标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @PutMapping(value = { "/warn" })
    public ResponseEntity<JsonObjectResult> updateWarnInf(@RequestBody @Valid EmergencyWarnReqInfo info) {

        emergencyPreService.updateWarnInf(info);
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "应急预案管理-修改预案启动标准(参数)接口")
    @ModuleService(totalModule = BigMenuConstants.emergencyPre, moduleName = ModuleConstants.ENERGENCYPREPLACE)
    @PutMapping(value = { "/warn/startup" })
    public ResponseEntity<JsonObjectResult> updateWarnstartupInf(@RequestBody @Valid EmergencyWarnStartUpReqInfo info) {

        emergencyPreService.updateWarnstartupInf(info);
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }

}
